package com.etalk.crm.controller.material;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.etalk.crm.pojo.TextbookesCategory;
import com.etalk.crm.pojo.Textbooks;
import com.etalk.crm.service.TextbooksService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教材
 * @author Jordan
 */
@Controller
@RequestMapping("/material")
public class MaterialController {

    @Resource
    private TextbooksService textbooksService;

    /**
     * 根据父id查询教材分类id和名称
     * @param parentId 父id
     * @return 教材分类列表
     */
    @ResponseBody
    @PostMapping(value = "/type/list/{parentId}",produces = "application/json; charset=utf-8")
    public String getMaterialType(@PathVariable("parentId") int parentId){
        Map<String,Object> resultMap=new HashMap<>(2);
        List<TextbookesCategory> list=textbooksService.searchTextbookCategoryByParentId(parentId);
        resultMap.put("status",1);
        resultMap.put("data",list);
        return JSON.toJSONString(resultMap);
    }

    @ResponseBody
    @PostMapping(value = "/list/{typeId}",produces = "application/json; charset=utf-8")
    public String getMaterialList(@PathVariable("typeId") int typeId){
        Map<String,Object> resultMap=new HashMap<>(2);
        List<Textbooks> list=textbooksService.selectTextbooksListByThirdId(typeId);
        resultMap.put("status",1);
        resultMap.put("data",list);
        return JSON.toJSONString(resultMap);
    }

    @ResponseBody
    @PostMapping(value = "/page/list/{materialId}",produces = "application/json; charset=utf-8")
    public String getMaterialPageList(@PathVariable("materialId") int materialId){
        Map<String,Object> resultMap=new HashMap<>(2);
        JSONArray list=textbooksService.getConfigByTextbooksId(materialId);
        resultMap.put("status",1);
        resultMap.put("data",list);
        return JSON.toJSONString(resultMap);
    }
}
